//
//  ViewController.m
//  work1
//
//  Created by zhoudong on 2018/5/15.
//  Copyright © 2018年 session. All rights reserved.
//
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import "ViewController.h"
#import "RequestNetwork.h"
#import "FileDownLoadNetWork.h"
 
@interface ViewController ()<FileDownLoadDelegate>{
    FileDownLoadNetWork* mDownFile;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

//-----------网络数据请求--------------------------
#pragma mark 网络请求
- (IBAction)btnClick:(id)sender {
    
    NSString* url = @"http://m.lanlingfuli.com/aif/home/getRecomm";
    NSString* session = @"23134543223";
    NSDictionary* data = @{@"agency":@"ios",@"pageIndex":@"1"};
    
    //data
    RequestNetwork* reqWork = [RequestNetwork new];
   [reqWork postReq:url sysSession:session reqData:data back:^(id result, NSString *msg, NSInteger errorCode) {
        //code=200 获取到服务器数据
        if(errorCode == 200){
           //文档
            
        }else{
//
            
        }
    }];
    
 
}

//------------------------------大文件下载------------------------------------
#pragma mark 开始下载
- (IBAction)startDown:(id)sender {
    
      [self downFile:NO];

}
#pragma mark 断点续传
- (IBAction)breakpointContinuingly:(id)sender {
    
    [self downFile:YES];
}


-(void)downFile:(BOOL)breakpoint{
    
    if(mDownFile == nil){
        NSString* fileUrl = @"http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4";
        //NSString* fileUrl = @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
        FileDownLoadNetWork* downFile = [FileDownLoadNetWork new];
        mDownFile = downFile;
        downFile.myDeleate = self;
        [downFile downFile:fileUrl isBreakpoint:breakpoint];
    }
    
    
}

#pragma mark 暂停继续
- (IBAction)suspendedDown:(id)sender {
    
    [mDownFile suspendDown];
}

#pragma mark 取消下载
- (IBAction)cancelDown:(id)sender {
    
     [mDownFile cancelDown];
    
    self.progress.progress = 0;
    mDownFile = nil;
}

//进度返回
-(void)backDownprogress:(float)progress{
    self.progress.progress = progress;
}

//下载成功
-(void)downSucceed:(NSURL*)url{
    
    
    [self paly: url];
    self.progress.progress = 0;
    
    self->mDownFile = nil;
}

//下载失败
-(void)downError:(NSError*)error{
    
    self->mDownFile = nil;
    self.progress.progress = 0;
    
    
     [[[UIAlertView alloc] initWithTitle:@"下载失败" message:@"请重新下载" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
    
}



//调用播放
-(void)paly:(NSURL*)playUrl{
    
    //系统的视频播放器
    AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
    //播放器的播放类
    AVPlayer * player = [[AVPlayer alloc]initWithURL:playUrl];
    
    controller.player = player;
    //自动开始播放
    [controller.player play];
    //推出视屏播放器
    // [self presentViewController:controller animated:YES completion:nil];
    
    
    [self  presentViewController:controller animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
